using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace UniFramework.RedPoint
{
    public class UniRedPointSystem
    {
        private Dictionary<ERedPointType, RedPointTreeNode> _redPointTreeDic;

        public UniRedPointSystem()
        {
            _redPointTreeDic = new Dictionary<ERedPointType, RedPointTreeNode>();
        }

        public void Dispose()
        {
            _redPointTreeDic.Clear();
            _redPointTreeDic = null;
        }

        /// <summary>
        /// 创建一个树节点
        /// </summary>
        /// <param name="treeNodeType"></param>
        /// <param name="fatherNodeType"></param>
        /// <returns></returns>
        public RedPointTreeNode CreateTreeNode(ERedPointType treeNodeType, ERedPointType fatherNodeType)
        {
            RedPointTreeNode fatherNode = null;
            _redPointTreeDic.TryGetValue(fatherNodeType, out fatherNode);
            var treeNode = new RedPointTreeNode();
            treeNode.Init(treeNodeType, fatherNode);
            fatherNode?.AddSonNode(treeNode);
            _redPointTreeDic.Add(treeNodeType, treeNode);
            return treeNode;
        }

        /// <summary>
        /// 获取一个树节点
        /// </summary>
        /// <param name="treeNodeType"></param>
        /// <returns></returns>
        public RedPointTreeNode GetTreeNode(ERedPointType treeNodeType)
        {
            RedPointTreeNode treeNode = null;
            _redPointTreeDic.TryGetValue(treeNodeType, out treeNode);
            return treeNode;
        }

        /// <summary>
        /// 移除一个树节点
        /// </summary>
        /// <param name="treeNodeType"></param>
        /// <returns></returns>
        public bool RemoveTreeNode(ERedPointType treeNodeType)
        {
            RedPointTreeNode treeNode = null;
            if (_redPointTreeDic.TryGetValue(treeNodeType, out treeNode))
            {
                treeNode.Dispose();
                _redPointTreeDic.Remove(treeNodeType);
                return true;
            }
            return false;
        }
    }
}